home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-04-19 | 2.6 KB | 131 lines | [TEXT/PJMM] |
- program FormulaTest;
-
- { test program to test the formular eval lib }
-
- uses
-
- DynamicMath;
-
- var
- theFormula: Str255;
- theText: CharsHandle;
- theErr: integer;
- theF: Formula;
- es, ps: Str255; (* error strings *)
- x: real;
- y: real;
- i: integer;
- t: real;
-
- win: WindowPtr;
- dlg: DialogPtr;
- r: Rect;
- xc, yc: integer;
- w: integer;
- dx: real;
- TwoPeriod: Real;
-
- dummy: boolean;
- theEvent: EventRecord;
-
- const
- cGraphWindowID = 400;
- cFormulaDialogID = 400;
- cBadFormulaAlertID = 900;
- cOKButton = 1;
- cFormulaTextID = 2;
-
- function RealOut (theReal: real): str255;
- var
- s, s1: Str255;
- n: integer;
- s2: Str255;
-
- begin
- if theReal < 0 then
- s1 := '-'
- else
- s1 := '';
- NumToString(TRUNC(theReal), s);
- theReal := theReal - TRUNC(theReal);
- n := ABS(TRUNC(theReal * 10000));
- NumToString(n, s2);
- s := Concat(s1, s, '.');
- if n < 1000 then
- s := Concat(s, '0');
- if n < 100 then
- s := Concat(s, '0');
- if n < 10 then
- s := Concat(s, '0');
-
- s := Concat(s, s2);
- RealOut := s;
- end;
-
- (* GetFormula : Display Dialog until item = OK, then read the item string *)
-
- function GetFormula: Str255;
- var
- Dlg: DialogPtr;
- itemHit: integer;
- dummyType: integer;
- theText: Handle;
- dummyRect: Rect;
- theFormula: Str255;
-
- begin
- Dlg := GetNewDialog(cFormulaDialogID, nil, pointer(-1));
- SetPort(Dlg);
- repeat
- ModalDialog(nil, itemHit);
- until itemHit = cOKButton;
- GetDItem(Dlg, cFormulaTextID, dummyType, theText, dummyRect);
- GetIText(theText, theFormula);
- DisposDialog(Dlg);
- GetFormula := theFormula;
- end;
-
-
- begin
- InitCursor;
- repeat
- theFormula := GetFormula
- until Length(theFormula) >= 1;
- theText := CharsHandle(NewHandle(SizeOf(Chars)));
- NEW(theF);
- Str2Text(theFormula, theText);
- theErr := Parse(theText, theF);
- if theErr <> noErr then
- begin
- NumToString(theErr, es);
- NumToString(gPos, ps);
- ParamText(es, ps, '', '');
- w := StopAlert(cBadFormulaAlertID, nil);
- ExitToShell;
- end;
-
- win := GetNEwWindow(cGraphWindowID, nil, pointer(-1));
- SetPort(win);
- TwoPeriod := 2 * Pi;
- r := win^.portrect;
- yc := ABS(r.bottom - r.top) div 2;
- xc := ABS(r.right - r.left) div 2;
- w := ABS(r.right - r.left);
- dx := TwoPeriod / w;
- x := -TwoPeriod / 2; (* from -1/2 T to + 1/2 T *)
- moveto(0, yc);
- LineTo(r.right, yc);
- moveto(xc, 0);
- LineTo(xc, r.bottom);
- moveto(0, yc - TRUNC(theF.evaluate(x, y)));
- for i := 1 to w do
- begin
- x := x + dx;
- LineTo(i, yc - TRUNC(theF.evaluate(x, y)));
- end;
- repeat
- dummy := GetNextEvent(everyEvent, theEvent);
- SystemTask;
- until (theEvent.what = keydown) or (theEvent.what = mouseDown);
- DisposeWindow(win);
- end.